import 'package:ladder_cash/app/com/view/common_view.dart';
import 'package:ladder_cash/generated/l10n.dart';
import 'package:permission_handler/permission_handler.dart';

import '../../../library/src/logger.dart';

class PermissionUtils {
  static Future<bool> checkCameraPermission() async {
    //获取当前的权限
    var status = await Permission.camera.status;
    if (status == PermissionStatus.granted) {
//已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.camera.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        showPermissionsDialog(
            title: S.current.cameraPermissionTitle,
            message: S.current.cameraPermissionMessage);
        return false;
      }
    }
  }

  Future<bool> requestCalendarPermission() async {
    //获取当前的权限
    var status = await Permission.phone.status;
    if (status == PermissionStatus.granted) {
//已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.phone.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        return false;
      }
    }
  }

  static Future<bool> checkSMSPermission() async {
    //获取当前的权限
    var status = await Permission.sms.status;
    if (status == PermissionStatus.granted) {
//已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.sms.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        showPermissionsDialog(
            title: S.current.smsPermissionTitle,
            message: S.current.smsaPermissionMessage);
        return false;
      }
    }
  }

  static Future<bool> checkCalendarPermission() async {
    //获取当前的权限
    var status = await Permission.calendar.status;
    if (status == PermissionStatus.granted) {
//已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.calendar.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        return false;
      }
    }
  }

  static Future<bool> checkWifiPermission() async {
    //获取当前的权限
    var status = await Permission.nearbyWifiDevices.status;
    if (status == PermissionStatus.granted) {
//已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.sms.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        return false;
      }
    }
  }


  static Future<bool> checkContactPermission(Function callback) async {
    // return true;//印度 不传
    //获取当前的权限
    var status = await Permission.contacts.status;
    if (status == PermissionStatus.granted) {
      callback();
//已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.contacts.request();

      ///上面没有发起请求。
      if (status == PermissionStatus.granted) {
        callback();
        return true;
      } else {
        // return true;
        showPermissionsDialog(
            title: S.current.contactPermissionTitle,
            message: S.current.contactPermissionMessage,
           callback: callback
        );
        return false;
      }
    }
  }
  static Future<bool> checkPhonePermission() async {
    //获取当前的权限
    var status = await Permission.phone.status;
    if (status == PermissionStatus.granted) {
//已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.phone.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        openAppSettings();
        return false;
      }
    }
  }
}
